﻿<Window x:Class="ProjectDependencyBrowser.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converters="clr-namespace:MyToolkit.Converters;assembly=MyToolkit.Extended"
        xmlns:viewModels="clr-namespace:ProjectDependencyBrowser.ViewModels"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:build="clr-namespace:MyToolkit.Build;assembly=MyToolkit.Extended"
        xmlns:converters1="clr-namespace:ProjectDependencyBrowser.Converters"
        mc:Ignorable="d"
        Title="Project Dependency Browser" Background="#EEEEEE"
        Height="700" Width="1200" MinHeight="700" MinWidth="1200"
        WindowStartupLocation="CenterScreen">

    <Window.Resources>
        <viewModels:MainWindowModel x:Key="ViewModel" />

        <converters:VisibilityConverter x:Key="VisibilityConverter" />
        <converters:NotConverter x:Key="NotConverter" />
        <converters1:ProjectTypesConverter x:Key="ProjectTypesConverter" />

        <DataTemplate x:Key="ProjectReferenceTemplate">
            <StackPanel d:DataContext="{d:DesignInstance Type=build:VsProject}" Margin="0,2,0,2" Background="Transparent">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Show projects with this project reference" 
                                  Command="{Binding SetProjectFilterCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                        <MenuItem Header="Open project directory in Explorer" 
                                  Command="{Binding OpenProjectDirectoryCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>

                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                <TextBlock Text="{Binding FileName}" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="NuGetPackageTemplate">
            <StackPanel d:DataContext="{d:DesignInstance Type=build:NuGetPackageReference}" Margin="0,2,0,2" Background="Transparent">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Show projects with this NuGet package" 
                                  Command="{Binding SetNuGetPackageFilterCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                        <MenuItem Header="Show on NuGet.org website" 
                                  Command="{Binding OpenNuGetWebsiteCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>

                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                <TextBlock Text="{Binding Version}" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AssemblyReferenceTemplate">
            <StackPanel d:DataContext="{d:DesignInstance Type=build:AssemblyReference}" Margin="0,2,0,2">
                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                <TextBlock Text="{Binding Version}" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="SolutionTemplate">
            <StackPanel d:DataContext="{d:DesignInstance Type=build:VsSolution}" Margin="0,2,0,2" Background="Transparent">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Show all projects in this solution" 
                                  Command="{Binding SetSolutionFilterCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                        <MenuItem Header="Open solution in Visual Studio" 
                                  Command="{Binding TryOpenSolutionCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>

                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                <TextBlock Text="{Binding Path}" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="SolutionListTemplate">
            <StackPanel d:DataContext="{d:DesignInstance Type=build:VsSolution}" Margin="0,2,0,2" Background="Transparent">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Show all projects in this solution" 
                                  Command="{Binding SetSolutionFilterCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                        <MenuItem Header="Open solution in Visual Studio" 
                                  Command="{Binding TryOpenSolutionCommand, Source={StaticResource ViewModel}}" 
                                  CommandParameter="{Binding}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>

                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                <TextBlock Text="{Binding FileName}" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

    <Grid DataContext="{StaticResource ViewModel}" Margin="8"
          IsEnabled="{Binding IsLoading, Converter={StaticResource NotConverter}}">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!-- Tabs -->
        <TabControl Margin="-8,0,-8,8" Height="164" x:Name="Tabs">
            <!-- Scan directory -->
            <TabItem Header="Root directory">
                <Grid Margin="8" VerticalAlignment="Top">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <StackPanel>
                        <TextBlock Text="Root directory: " FontWeight="Bold" Margin="0,0,0,4" />
                        <TextBox Text="{Binding RootDirectory, Mode=TwoWay}" />
                    </StackPanel>

                    <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="0,12,0,0" Orientation="Horizontal">
                        <StackPanel Margin="0,0,16,0">
                            <CheckBox IsChecked="{Binding IgnoreExceptions, Mode=TwoWay}" 
                                      Content="Ignore exceptions when scanning" Margin="0,0,0,8" />
                            <CheckBox IsChecked="{Binding MinimizeWindowAfterSolutionLaunch, Mode=TwoWay}" 
                                      Content="Minimize window after launching a solution" Margin="0,0,0,8" />
                            <CheckBox IsChecked="{Binding AutomaticallyScanDirectory, Mode=TwoWay}" 
                                      Content="Automatically scan directory at startup" />
                        </StackPanel>
                        <StackPanel>
                            <CheckBox IsChecked="{Binding EnableShowApplicationHotKey, Mode=TwoWay}" 
                                      Content="Enable hotkey (Ctrl-Win-V) to show application (needs restart)" Margin="0,0,0,8" />
                        </StackPanel>
                    </StackPanel>

                    <Button Content="Select directory..." Click="OnSelectDirectory" 
                            Grid.Column="1" Grid.Row="0"
                            Padding="8,4,8,4" Margin="8,4,0,0" />

                    <Button Content="Load projects &amp; solutions" Command="{Binding LoadProjectsCommand}" 
                            Grid.Column="2" Grid.Row="0"
                            Padding="8,4,8,4" Margin="8,4,0,0" />
                </Grid>
            </TabItem>

            <!-- Filters -->
            <TabItem Header="Filter projects" IsEnabled="{Binding IsLoaded}">
                <Grid Margin="8">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <StackPanel>
                        <TextBlock Text="Project name: " FontWeight="Bold" Margin="0,0,0,4" />
                        <TextBox Text="{Binding Filter.ProjectNameFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                Margin="0,0,0,8" x:Name="ProjectNameFilter" GotFocus="OnProjectNameFilterGotFocus"
                                KeyDown="OnKeyDown" PreviewKeyDown="OnPreviewKeyDown" AcceptsReturn="False" />
                    </StackPanel>

                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <StackPanel Margin="0,0,4,0" Grid.Column="0">
                            <CheckBox IsChecked="{Binding Filter.IsSolutionFilterEnabled, Mode=TwoWay}" 
                                      Content="Filter by solution" FontWeight="Bold" />
                            <ComboBox ItemsSource="{Binding AllSolutions}" 
                                      ItemTemplate="{StaticResource SolutionTemplate}"
                                      Visibility="{Binding Filter.IsSolutionFilterEnabled, Converter={StaticResource VisibilityConverter}}"
                                      SelectedItem="{Binding Filter.SolutionFilter, Mode=TwoWay, TargetNullValue=null}" Margin="0,8,0,0" />
                        </StackPanel>
                        <StackPanel Margin="4,0,4,0" Grid.Column="1">
                            <CheckBox IsChecked="{Binding Filter.IsNuGetFilterEnabled, Mode=TwoWay}" 
                                      Content="Filter by installed NuGet package" FontWeight="Bold" />
                            <ComboBox ItemsSource="{Binding UsedNuGetPackages}" 
                                      ItemTemplate="{StaticResource NuGetPackageTemplate}"
                                      Visibility="{Binding Filter.IsNuGetFilterEnabled, Converter={StaticResource VisibilityConverter}}"
                                      SelectedItem="{Binding Filter.NuGetPackageFilter, Mode=TwoWay}" Margin="0,8,0,0" />
                        </StackPanel>
                        <StackPanel Margin="4,0,4,0" Grid.Column="2">
                            <CheckBox IsChecked="{Binding Filter.IsProjectReferenceFilterEnabled, Mode=TwoWay}" 
                                      Content="Filter by referenced project" FontWeight="Bold" />
                            <ComboBox ItemsSource="{Binding UsedProjectReferences}" 
                                      ItemTemplate="{StaticResource ProjectReferenceTemplate}"
                                      Visibility="{Binding Filter.IsProjectReferenceFilterEnabled, Converter={StaticResource VisibilityConverter}}"
                                      SelectedItem="{Binding Filter.ProjectReferenceFilter, Mode=TwoWay, TargetNullValue=null}" Margin="0,8,0,0" />
                        </StackPanel>
                        <StackPanel Margin="4,0,0,0" Grid.Column="3">
                            <CheckBox IsChecked="{Binding Filter.ShowOnlyProjectsWithoutSolution, Mode=TwoWay}" 
                                      Content="Only projects without solution" Margin="0,0,0,8" />
                            <CheckBox IsChecked="{Binding Filter.ShowOnlyProjectsWithMultipleSolutions, Mode=TwoWay}" 
                                      Content="Only projects with multiple solutions" Margin="0,0,0,8" />
                            <CheckBox IsChecked="{Binding Filter.ShowOnlyProjectsWithNuGetPackages, Mode=TwoWay}" 
                                      Content="Only projects with installed NuGet packages" Margin="0,0,0,8" />
                        </StackPanel>

                        <Button Command="{Binding ClearFilterCommand}" Padding="8" Grid.Column="4" Margin="0,0,0,4">
                            <TextBlock>
                                Clear<LineBreak />filters
                            </TextBlock>
                        </Button>
                    </Grid>
                </Grid>
            </TabItem>

            <!-- About -->
            <TabItem Header="About">
                <StackPanel Margin="8">
                    <TextBlock Margin="0,0,0,8" FontSize="14">
                        <Hyperlink NavigateUri="http://projectdependencybrowser.codeplex.com" Click="OnOpenHyperlink">Project Dependency Browser</Hyperlink>
                        has been developed by 
                        <Hyperlink NavigateUri="http://www.rsuter.com" Click="OnOpenHyperlink">Rico Suter</Hyperlink>.
                    </TextBlock>
                    <TextBlock Margin="0,0,0,8">
                        Version: <Run Text="{Binding ApplicationVersion, Mode=OneWay}" />
                        <LineBreak />
                        License: <Hyperlink NavigateUri="https://projectdependencybrowser.codeplex.com/license" Click="OnOpenHyperlink">Ms-PL</Hyperlink>
                    </TextBlock>
                    <TextBlock Text="Components" FontWeight="Bold" Margin="0,4,0,4" />
                    <TextBlock Margin="0,0,0,4">
                        - <Hyperlink NavigateUri="http://mytoolkit.codeplex.com" Click="OnOpenHyperlink">MyToolkit</Hyperlink>: 
                          .NET library with MVVM classes, controls and more components.
                    </TextBlock>
                </StackPanel>
            </TabItem>
        </TabControl>

        <!-- Projects -->
        <TextBlock Text="No projects have been loaded. " Grid.Row="2"
                   Visibility="{Binding IsLoaded, Converter={StaticResource NotConverter}}" />
        <Grid Grid.Row="2" Visibility="{Binding IsLoaded, Converter={StaticResource VisibilityConverter}}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" MaxWidth="350" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock FontWeight="Bold" Margin="0,0,0,4">
                    Projects 
                    (<Run Text="{Binding FilteredProjects.Count, Mode=OneWay}" /> / 
                     <Run Text="{Binding AllProjects.Count, Mode=OneWay}" />)
                </TextBlock>
                <ListBox Grid.Row="1" x:Name="ProjectList" KeyDown="OnKeyDown"
                         SelectionMode="Single" HorizontalContentAlignment="Stretch"
                         ItemsSource="{Binding FilteredProjects}" 
                         SelectedItem="{Binding SelectedProject, Mode=TwoWay}"
                         ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                         ItemTemplate="{StaticResource ProjectReferenceTemplate}" />
            </Grid>

            <!-- Selected project -->
            <Grid Grid.Column="1" Margin="8,0,0,0" Visibility="{Binding SelectedProject, Converter={StaticResource VisibilityConverter}}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Text="Project" FontWeight="Bold" Margin="0,0,0,4" />
                <Border Background="White" BorderBrush="#999999" BorderThickness="1" Grid.Row="1">
                    <Grid Margin="8">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <StackPanel Grid.ColumnSpan="4">
                            <Grid Margin="0,0,0,8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.25*" />
                                    <ColumnDefinition Width="0.75*" />
                                </Grid.ColumnDefinitions>

                                <StackPanel Grid.Column="0">
                                    <TextBlock Text="Name: " FontWeight="Bold" Margin="0,0,0,4" />
                                    <TextBlock Text="{Binding SelectedProject.Name}" />
                                </StackPanel>
                                <StackPanel Grid.Column="1">
                                    <TextBlock Text="Namespace: " FontWeight="Bold" Margin="0,0,0,4" />
                                    <TextBlock Text="{Binding SelectedProject.Namespace}" />
                                </StackPanel>
                                
                                <StackPanel Orientation="Horizontal" 
                                            HorizontalAlignment="Right" VerticalAlignment="Top" 
                                            Grid.Column="0" Grid.ColumnSpan="2">
                                    <Button Content="Open project directory" 
                                            Command="{Binding OpenProjectDirectoryCommand}"
                                            CommandParameter="{Binding SelectedProject}" 
                                            Padding="8,4,8,4" Margin="0,0,8,0" />

                                    <Button Content="Analyze dependencies" Padding="8,4,8,4"
                                            Command="{Binding AnalyzeProjectDependenciesCommand}"
                                            CommandParameter="{Binding SelectedProject}" />
                                </StackPanel>
                            </Grid>

                            <TextBlock Text="Path: " FontWeight="Bold" Margin="0,0,0,4" />
                            <TextBlock Text="{Binding SelectedProject.Path}" Margin="0,0,0,8" />

                            <StackPanel Visibility="{Binding SelectedProject.ProjectTypes, Converter={StaticResource VisibilityConverter}}">
                                <TextBlock Text="Project type: " FontWeight="Bold" Margin="0,0,0,4" />
                                <TextBlock Text="{Binding SelectedProject.ProjectTypes, Converter={StaticResource ProjectTypesConverter}}" Margin="0,0,0,8" />
                            </StackPanel>
                        </StackPanel>

                        <TextBlock Grid.Row="1" Grid.Column="0" 
                                   Text="Solutions: " FontWeight="Bold" Margin="0,0,0,4" />
                        <ListBox Grid.Row="2" Grid.Column="0" Margin="0,0,4,0" 
                                 MouseDoubleClick="OnSolutionDoubleClicked"
                                 KeyUp="OnSolutionKeyUp" HorizontalContentAlignment="Stretch"
                                 ItemTemplate="{StaticResource SolutionListTemplate}"
                                 ItemsSource="{Binding SelectedProjectSolutions}" />

                        <TextBlock Grid.Row="1" Grid.Column="1" 
                                   Text="Referenced projects: " FontWeight="Bold" Margin="0,0,0,4" />
                        <ListBox Grid.Row="2" Grid.Column="1" Margin="0,0,4,0" 
                                 MouseDoubleClick="OnProjectDoubleClicked"
                                 KeyUp="OnProjectKeyUp" HorizontalContentAlignment="Stretch"
                                 ItemTemplate="{StaticResource ProjectReferenceTemplate}"
                                 ItemsSource="{Binding SelectedProject.ProjectReferences}" />

                        <TextBlock Grid.Row="1" Grid.Column="2" 
                                   Text="NuGet packages: " FontWeight="Bold" Margin="4,0,0,4" />
                        <ListBox Grid.Row="2" Grid.Column="2" Margin="4,0,4,0" 
                                 HorizontalContentAlignment="Stretch"
                                 ItemTemplate="{StaticResource NuGetPackageTemplate}"
                                 ItemsSource="{Binding SelectedProject.NuGetReferences}" />

                        <TextBlock Grid.Row="1" Grid.Column="3" 
                                   Text="Referenced assemblies: " FontWeight="Bold" Margin="4,0,0,0" />
                        <ListBox Grid.Row="2" Grid.Column="3" Margin="4,0,0,0" 
                                 HorizontalContentAlignment="Stretch"
                                 ItemTemplate="{StaticResource AssemblyReferenceTemplate}"
                                 ItemsSource="{Binding SelectedProject.AssemblyReferences}" />
                    </Grid>
                </Border>
            </Grid>
        </Grid>
    </Grid>
</Window>
